import { size, IFormProps, pageSize, dateTimeFormat } from "../../utils/common";
import React from "react";
import { connect } from "dva";
import { Form, Row, Col, Button, Input, Table, Select } from 'antd';
import { PaginationProps } from "antd/lib/pagination";
import moment from 'moment';
const Styles = require("../../styles/index.less");
import * as api from '../../api/api';
import DividendsEdit from "../../components/dividends/DividendsEdit";

export interface IDividendPros extends IFormProps {
    dividend: any
}
class Dividends extends React.PureComponent<IDividendPros, any>{
    constructor(props) {
        super(props);
        this.state = {}
    }

    search = () => {
        this.getDividends();
    }

    getDividends = (payload?) => {
        payload = Object.assign({}, this.props.dividend.commissionsSearchs, this.props.form.getFieldsValue(), { ...payload });
        this.props.dispatch({
            type: "dividend/getDividends",
            payload
        });
    }
    add = () => {
        this.edit();
    }
    reset = () => {
        this.props.form.resetFields();
    }

    edit = (dividend = {}) => {
        this.props.dispatch({
            type: "dividend/save",
            payload: { dividend }
        });
        this.setVisible(true);
    }

    setVisible = (visible = false) => {
        this.setState({ visible });
    }

    save = (form) => {
        const { id } = this.props.dividend.dividend;
        form = Object.assign({}, form, { id });
        this.props.dispatch({
            type: "dividend/saveDividends",
            payload: {
                form,
                callback: () => {
                    this.setVisible();
                }
            }
        });
    }

    render(): JSX.Element {
        const { getFieldDecorator } = this.props.form;
        const { loading } = this.props;
        const { dividend, dividends: { totalCount, items }, dividendSearchs: { pageIndex, pageSize } } = this.props.dividend;
        const pageChange = (pageIndex, pageSize) => {
            let payload = Object.assign({},
                this.props.dividend.dividendSearchs,
                { pageIndex, pageSize });
            this.getDividends(payload);
        }

        const pagination: PaginationProps | boolean = {
            total: totalCount,
            defaultCurrent: 1,
            current: pageIndex,
            pageSize: pageSize,
            showSizeChanger: true,
            showTotal: (total) => `共 ${total} 条`,
            showQuickJumper: true,
            pageSizeOptions: ["10"],
            onShowSizeChange: pageChange,
            onChange: pageChange
        };

        return (
            <Row className={Styles.F_BG_WHITE1}>
                <Row className={Styles.F_BG_WHITE}>
                    <Col span={24}>
                        <Form layout="inline">
                        <Form.Item>
                                <Button size={size} onClick={this.edit}>新增</Button>
                            </Form.Item>
                            <Form.Item>
                                <Button size={size} type="primary" loading={loading} onClick={this.search}>查询</Button>
                            </Form.Item>
                        </Form>
                        <Col span={24}>
                            <hr className={Styles.hrBorder} />
                            <h3>分红比列</h3>
                        </Col>
                        <Col span={24}>
                            <Table
                                dataSource={items}
                                bordered
                                pagination={pagination}
                                rowKey={(record: api.SingleUserMallDividendsVODividendVO) => String(record.id)}
                                loading={loading}
                            >
                                <Table.Column title="序号" key="no" render={(text, record, index) => {
                                    return index + 1
                                }} />
                                <Table.Column title="期限" key="timelimit" dataIndex="timelimit" />
                                <Table.Column title="分红比列" key="dividendAmountView" render={(record: any) => {
                                    return record.dividendAmountView + '%';
                                }} />
                                <Table.Column title="分红方式" key="displayType" render={(text, record: api.SingleUserMallDividendsVODividendVO, index) => {
                                    return Number(record.dividendTypes) == 0 ? '现金'  : ''
                                }} />
                                <Table.Column title="操作" render={(text, record: api.SingleUserMallDividendsVODividendVO, index) => {
                                    return (
                                        <div>
                                            <a className={Styles.M_RIGHT} onClick={() => this.edit(record)}>
                                                编辑
                                            </a>
                                        </div>
                                    );
                                }} />
                            </Table>
                        </Col>
                    </Col>
                </Row>
                {
                    this.state.visible ? (<DividendsEdit visible={this.state.visible}
                        dividend={dividend}
                        onSave={this.save}
                        onCancel={this.setVisible} />) :
                        (undefined)
                }
            </Row>)
    }
}

export default connect(({ dividend, loading: { models: { dividend: loading } } }) => ({ dividend, loading }))(Form.create()(Dividends));
